Mathematica has a built in function called D[] that will perform all the symbolic differentiations in the chapter. Before we use D[], however, we will develop our own function "diff" to perform symbolic differentiation. In doing this you will be able to see how powerful each of these rules is. You will see what the various rules can do and what they can not do, by seeing the effect of adding new rules.
We will be able to perform all the symbolic differentiations in the chapter by writing only 16 general symbolic function rules in our program. Our "diff" program is built up in steps that correspond to the rules in Chapter 6. As you <Enter> cells, you are adding more rules to "diff," just as you yourself are developing more powerful differentiation procedures in the corresponding sections of the text. For example, formulas that cannot be differentiated without the product rule also cannot be found with "diff" before you <Enter> that section of the program. They can be computed with "diff" after you <Enter> the product rule. There are problems just like this in the text for you to compute or say why the rules are not powerful enough to do so.
The first rules we define allow Mathematica to differentiate powers. The first rule below almost does this, but there are computer complications. Note the underscores after the expressions supplied to the diff function. The . after the n tells Mathematica that there doesn't need to be a number n given to the diff function. This provides for the case when we are differentiating x^1, which we usually just write as x. The second rule says the derivative of a constant c is zero, provided c doesn't depend on x. (See Mathematica's manual for more explanation of the peculiar "?NumberQ" command, essentially, it asks if c is a number and not a function of x.)
:[font = input; preserveAspect; ]
diff[ x_^n_., x_ ] := n x^(n-1) ;
diff[ c_?NumberQ, x_ ] := 0 ;
:[font = text; inactive; preserveAspect; ]
Once you have Entered the rules above you can differentiate powers as in the next two cells. (<Enter> them now.)
:[font = input; preserveAspect; ]
diff[ x^(1/5), x ]
:[font = text; inactive; preserveAspect; ]
Since the Sqrt function is a disguised power, we can use the rules above to differentiate that as well. See this by Entering the next cell's computation.
:[font = input; preserveAspect; ]
diff[ Sqrt[x], x ]
:[font = text; inactive; preserveAspect; ]
One odd thing about Mathematica is that it does not consider Pi and E to be `numbers.' (Pi and E have many symbolic uses in Mathematica.) Thus the rule for numbers above will not apply to Pi or E.
We must explicitly define the derivates of Pi and E to be 0 as follows. <Enter> the next rule now, so our power rule is complete.
:[font = input; preserveAspect; ]
diff[ Pi, x_ ] := 0 ;
diff[ E, x_ ] := 0 ;
:[font = text; inactive; preserveAspect; ]
Notice that Mathematica is still unable to differentiate some simple expressions. It still doesn't know that the derivative of a sum is the sum of the derivatives, because we haven't told it the superposition rule yet. Enter the next computation to see what happens when Mathematica doesn't know how to compute an answer.
:[font = input; preserveAspect; ]
diff[ 3 x + 2, x ]
:[font = text; inactive; preserveAspect; ]
In the blank cell below, use "diff" to compute the separate derivative dy/dx for y = 3 x. Then insert your own input cell and use "diff" to compute dz/dx for z = 2.
Now we define rules that will differentiate linear combinations of functions. We break this into two parts. Given a function times a constant a, "diff" is defined simply as the constant times "diff" of the function. Next, we define "diff" of a sum as the sum of the "diff"s. Notice that g itself can be any function, even another sum of functions. If g itself is a sum, Mathematica will simply apply the rule again.
<Enter> these rules now and continue.
:[font = input; preserveAspect; ]
diff[ a_?NumberQ f_, x_ ] := a diff[f, x] ;
diff[ f_ + g_, x_] := diff[f, x] + diff[g, x] ;
:[font = text; inactive; preserveAspect; ]
Now Mathematica is able to differentiate the simple sum we gave it above. <Enter> it.
:[font = input; preserveAspect; ]
diff[ 3 x + 2, x ]
:[font = text; inactive; preserveAspect; ]
To see how powerful these rules are, enter the following cell to differentiate
Next we will define the product rule for differentiation.
:[font = input; preserveAspect; ]
diff[ f_ g_, x_] := diff[f, x] g + f diff[g, x] ;
:[font = text; inactive; preserveAspect; ]
Now we can differentiate the product given above.
:[font = input; preserveAspect; ]
diff[ x Sin[x], x ]
:[font = text; inactive; preserveAspect; ]
Again to emphasize how powerful this rule is, note that g above itself can be a product of two functions. If it is, Mathematica will simply use the same rule again.
The last rule we need is the Chain Rule. Doing this is a little more complicated, but if we remember what the chain rule says, the rules below will be clear.
Suppose we wanted to differentiate the following function:
y = (x^2 + 2 x - 1)^3
To use the chain rule we would first differentiate using the power rule and then multiply by the derivative of the function x^2 + 2 x - 1 to get:
dy/dx = 3 (x^2 + 2 x - 1)^2 (2 x + 2).
This suggests the following rule for differentiation. <Enter> it.
:[font = input; preserveAspect; ]
diff[ f_^n_, x_ ] := n f^(n-1) diff[f, x] ;
:[font = text; inactive; preserveAspect; ]
This is the Chain Rule applied to a function to a power. Test it with the cell below.
:[font = input; preserveAspect; ]
diff[ (x^2 + 2 x - 1)^3, x ]
:[font = text; inactive; preserveAspect; ]
Once the form of the above rule is clear, it should be clear how to finish defining the chain rule. We simply modify the rules above for special functions of x to apply to functions of functions of x. Below are the Sine, Cosine, Log, and Exponential Rules redefined for arbitrary functions f.
The Superposition Rule and Product Rule remain unaltered and Mathematica can use them all. The function "diff" can now do the quotient rule as follows.
There is not much need to use the Quotient Rule once we have the above rules working properly. As another illustration of how powerful the above rules are, however, we will use the above rules to obtain the Quotient Rule in explicit form.
:[font = input; preserveAspect; ]
diff[ f g^-1, x ]
:[font = text; inactive; preserveAspect; ]
This looks a little odd, but if we use the Together command we will see the Quotient Rule in the text.
You know that Tan[x] = Sin[x]/Cos[x] and Mathematica 2.0 knows that, too. Unfortunately, it knows it too well. Try to compute the derivative of tangent using the diff[] rules so far:
With all the cells above <Enter>ed, you have written a general symbolic differentiation function that can do all the exercises that you are supposed to be able to do by the end of Chapter 6 on Symbolic Differentiation. (Mathematica's built-in D[.] also knows derivatives for non-elementary functions like Bessel functions.)